package com.maomao.admin.service.system.impl;

import com.maomao.admin.common.utils.DateUtils;
import com.maomao.admin.domain.core.vo.TableDataInfo;
import com.maomao.admin.domain.system.entry.QSysLoginInfo;
import com.maomao.admin.domain.system.entry.SysLoginInfo;
import com.maomao.admin.persistence.repository.SysLoginInfoRepository;
import com.maomao.admin.service.system.AbstractPageService;
import com.maomao.admin.service.system.ISysLoginInfoService;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysLoginInfoServiceImpl extends AbstractPageService implements ISysLoginInfoService {

    @Autowired
    private SysLoginInfoRepository loginInfoRepository;

    @Resource
    private JPAQueryFactory queryFactory;

    /**
     * 新增系统登录日志
     *
     * @param loginInfo 访问日志对象
     */
    @Override
    public void insertLoginInfo(SysLoginInfo loginInfo) {
        loginInfoRepository.save(loginInfo);
    }


    /**
     * 查询系统登录日志集合
     *
     * @param loginInfo 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public TableDataInfo<SysLoginInfo> selectLoginInfoPage(SysLoginInfo loginInfo) {
        JPAQuery<SysLoginInfo> sysLoginInfoJPAQuery = this.createJPAQuery(loginInfo);
        return new TableDataInfo<>(sysLoginInfoJPAQuery.fetch(), sysLoginInfoJPAQuery.fetchCount());
    }

    private JPAQuery<SysLoginInfo> createJPAQuery(SysLoginInfo loginInfo) {
        // 拼装条件
        List<Predicate> predicates = new LinkedList<>();
        if (StringUtils.isNotBlank(loginInfo.getIpaddr())) {
            predicates.add(QSysLoginInfo.sysLoginInfo.ipaddr.contains(loginInfo.getIpaddr()));
        }
        if (StringUtils.isNotBlank(loginInfo.getStatus())) {
            predicates.add(QSysLoginInfo.sysLoginInfo.status.eq(loginInfo.getStatus()));
        }
        if (StringUtils.isNotBlank(loginInfo.getUserName())) {
            predicates.add(QSysLoginInfo.sysLoginInfo.userName.eq(loginInfo.getUserName()));
        }
        if (loginInfo.getParams() != null && loginInfo.getParams().get("beginTime") != null && StringUtils.isNotBlank(loginInfo.getParams().get("beginTime").toString())) {

            predicates.add(QSysLoginInfo.sysLoginInfo.loginTime.gt(DateUtils.parseDate(loginInfo.getParams().get("beginTime").toString())));
        }
        if (loginInfo.getParams() != null && loginInfo.getParams().get("endTime") != null && StringUtils.isNotBlank(loginInfo.getParams().get("endTime").toString())) {

            predicates.add(QSysLoginInfo.sysLoginInfo.loginTime.gt(DateUtils.parseDate(loginInfo.getParams().get("endTime").toString())));
        }
        JPAQuery<SysLoginInfo> sysLoginInfoJPAQuery = queryFactory.selectFrom(QSysLoginInfo.sysLoginInfo);

        if (predicates.size() > 0) {
            sysLoginInfoJPAQuery = sysLoginInfoJPAQuery.where(predicates.toArray(new Predicate[0]));
        }
        return sysLoginInfoJPAQuery;
    }

    /**
     * 查询系统登录日志集合
     *
     * @param loginInfo 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLoginInfo> selectLoginInfoList(SysLoginInfo loginInfo) {
        JPAQuery<SysLoginInfo> sysLoginInfoJPAQuery = this.createJPAQuery(loginInfo);
        return sysLoginInfoJPAQuery.fetch();
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLoginInfoByIds(Long[] infoIds) {
        loginInfoRepository.deleteAllById(Arrays.asList(infoIds));
        return 1;
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLoginInfo() {
        loginInfoRepository.deleteAll();
    }
}
